#include <iostream>
#include <sstream>
#include "Serveur.h"
#define SERVEUR_COORD 0
#define CLIENT_COORD 1

using namespace std;
using namespace sf;

Serveur::Serveur() : score(0)
{
    raquetteServeur = new Raquette(1,250, sf::Color::Red);
    raquetteClient = new Raquette(773,250, sf::Color::Cyan);
    balle=new Balle();

     font.loadFromFile("arial.ttf");
}

void Serveur::showIPs()
{
    cout << "IP Locale : " << IpAddress::getLocalAddress() << endl;
    cout << "IP Sortante : " << IpAddress::getPublicAddress() << endl;
    cout << "En attente de connection ";
}

void Serveur::sendCoordRaquette(bool serveur, TcpSocket &socket)
{
    Uint32 posy;
    Uint32 vide = 0;
    Uint32 qui;
    if(serveur == SERVEUR_COORD){
        qui=SERVEUR_COORD;
        posy = raquetteServeur->getY();
    }
    else{
        qui = CLIENT_COORD;
        posy = raquetteClient->getY();
    }

    Packet packet;
    packet << qui << vide << posy << vide << vide;

    socket.send(packet);
}

void Serveur::sendCoordBalle(Balle* balle, TcpSocket &socket)
{
    Uint32 posx = balle->getX();
    Uint32 posy = balle->getY();
    Uint32 vide = 0;
    Uint32 qui = 2;


    Packet packet;
    packet << qui << posx << posy << vide << vide;

    socket.send(packet);
}

void Serveur::sendScore(Serveur* serveur, Client* client, TcpSocket &socket)
{
    Uint32 vide = 0;
    Uint32 qui = 3;
    Uint32 scorej1 = serveur->getScore();
    Uint32 scorej2 = client->getScore();

    Packet packet;
    packet << qui << vide << vide << scorej1 << scorej2;

    socket.send(packet);

}

Text Serveur::getScoreText()
{
    string scorestring;
    std::stringstream out;
    out << score;
    scorestring = out.str();

    Text text(scorestring, font, 50);
    text.setPosition(Vector2f(300, 25));
    return text;
}


void Serveur::increaseScore(){score+=1;}
void Serveur::setScore(int i){score=i;}
int Serveur::getScore(){return score;}
Balle* Serveur::getBalle(){ return balle;}
Raquette* Serveur::getRaquetteClient(){ return raquetteClient;}
Raquette* Serveur::getRaquetteServeur(){ return raquetteServeur;}
